package com.example.springbooksystem.config;

import com.example.springbooksystem.model.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-01-31
 * @time: 13:18:33
 */
@Slf4j
@ControllerAdvice
@ResponseBody // 告诉返回的是数据, 而不是视图
public class ErrorAdvice {
    @ExceptionHandler
    public Object excption(Exception e) {
        log.error("未知的错误, e: {}",e);
        return ResponseData.fatal("内部错误");
    }


    @ExceptionHandler
    public Object excption(NullPointerException e) {
        log.error("NullPointerException, e: {}",e);
        return ResponseData.fatal("NullPointerException");
    }

    @ExceptionHandler
    public Object excption(ArithmeticException e) {
        log.error("ArithmeticException, e: {}",e);
        return ResponseData.fatal("ArithmeticException");
    }
}
